/*
 * H264FramedLiveSource.cpp
 *
 *  Created on: 2014-4-9
 *      Author: root
 */

#include "H264FramedLiveSource.hh"

H264FramedLiveSource::H264FramedLiveSource( UsageEnvironment& env,
	char const* fileName,
	unsigned preferredFrameSize,
	unsigned playTimePerFrame )
	: FramedSource(env)
{
	video_cam = VideoCamera::createNew(320,240);
     //fp = fopen( fileName, "rb" );
    video_cam->CameraInit("/dev/video15",15);
}

H264FramedLiveSource* H264FramedLiveSource::createNew( UsageEnvironment& env,
	                                       char const* fileName,
	                                       unsigned preferredFrameSize /*= 0*/,
	                                       unsigned playTimePerFrame /*= 0*/ )
{
	H264FramedLiveSource* newSource = new H264FramedLiveSource(env, fileName, preferredFrameSize, playTimePerFrame);

	return newSource;
}

H264FramedLiveSource::~H264FramedLiveSource()
{
	//fclose(fp);
	video_cam->CameraDestory();
	delete video_cam;
}


long filesize(FILE *stream)
{
	long curpos, length;
	curpos = ftell(stream);
	fseek(stream, 0L, SEEK_END);
	length = ftell(stream);
	fseek(stream, curpos, SEEK_SET);
	return length;
}

void H264FramedLiveSource::doGetNextFrame()
{
	video_cam->GetNextFrame();
	//TwoWayCamera.GetNextFrame();
	fFrameSize =  video_cam->outFrameLen;
	if( fFrameSize >  fMaxSize)
	{
	        fNumTruncatedBytes = fFrameSize - fMaxSize;
            fFrameSize = fMaxSize;
     }
    else
	{
        fNumTruncatedBytes = 0;
    }

	memmove(fTo, video_cam->pOutFrame, fFrameSize);

	nextTask() = envir().taskScheduler().scheduleDelayedTask( 0,
		(TaskFunc*)FramedSource::afterGetting, this);//表示延迟0秒后再执行 afterGetting 函数
	return;
}



